//
//  NetRequest.swift
//  CocoaPodsCollect
//
//  Created by LiynXu on 2016/9/26.
//  Copyright © 2016年 LiynXu. All rights reserved.
//

import Foundation

import AFNetworking

protocol NetRequestDelagate {
    func finish(responseObject:AnyObject?)
    func failred(error:NSError)
}


class NetRequest: AFHTTPSessionManager {
    
    //单例
    var delegate:NetRequestDelagate?
    
    static let shareNetRequest:NetRequest = {
        let shareNetRequest:NetRequest = NetRequest()
        //规定了返回数据的类型为二进制数据
        
        //shareNetRequest.responseSerializer = AFHTTPResponseSerializer()
        return shareNetRequest
    }()
    
}

extension NetRequest{
    func GetRequest(url:String,parameters:[String:AnyObject]?) {
        GET(url, parameters: parameters, progress: nil, success: { (task:NSURLSessionDataTask,responseObject:AnyObject?) in
            
            //print(responseObject)
            
            guard self.delegate != nil else{
            print("delegate不存在")
                return
            }
             self.delegate?.finish(responseObject)
            
        }) { (task:NSURLSessionDataTask?, error:NSError) in
            
            guard self.delegate != nil else{
                print("delegate不存在")
                return
            }
            self.delegate?.failred(error)
        }
        
        
    }
    
}
